package edu.cuit.jead.mynetdisk.config;

import edu.cuit.jead.mynetdisk.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(4600);
    }
    private TokenInterceptor tokenInterceptor;
    public WebMvcConfig(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer){
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();

        excludePath.add("/login");
        excludePath.add("/register");
        excludePath.add("/userdata");
       // excludePath.add("/files/upload");
        excludePath.add("/img/**");  //静态资源
        excludePath.add("/song/**");  //静态资源
        excludePath.add("/files/download/**");
        excludePath.add("/files/toggle-star");
        excludePath.add("/files/move-to-trash");
        excludePath.add("/files/share-link");
        excludePath.add("/files/delete-permanently");
        excludePath.add("/changepictrue");
        excludePath.add("/E:/java/java_project/files/pictures/123@qq.com.jpg");
        excludePath.add("/update-username");
        excludePath.add("/update-password");

        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        //WebMvcConfigurer.super.addInterceptors(registry);
    }
}
